package com.example.widgetapplication;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import android.widget.Toast;

import java.util.Calendar;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener {
    private int year,month,day,mymonth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        Button btn1 = findViewById(R.id.btn_YS);
        btn1.setOnClickListener(this);

        Button btn2 = findViewById(R.id.btn_XZ);
        btn2.setOnClickListener(this);

        Button btn3 = findViewById(R.id.btn_RL);
        btn3.setOnClickListener(this);

        Button btn4 = findViewById(R.id.btn_XZs);
        btn4.setOnClickListener(this);

        Button btn5 = findViewById(R.id.btn_ZDY);
        btn5.setOnClickListener(this);
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_YS:
                AlertDialog alertDialog1 = new AlertDialog.Builder(DialogActivity.this).create();
                alertDialog1.setIcon(R.drawable.ic_launcher_background);
                alertDialog1.setTitle("确认拨打如下号码：");
                alertDialog1.setMessage("13568740021");
                alertDialog1.setButton(DialogInterface.BUTTON_NEGATIVE, "NO", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogActivity.this, "您取消了拨打!", Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog1.setButton(DialogInterface.BUTTON_POSITIVE, "YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogActivity.this, "电话正在拨通中...", Toast.LENGTH_SHORT).show();
                    }
                });
                alertDialog1.show();
                break;

            case R.id.btn_XZ:
                final String[] items = new String[]{"炸鸡","美蛙","牛排","汉堡","泡面"};
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setTitle("你最喜欢的食物：");
                builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogActivity.this,"您选择了" + items[i],Toast.LENGTH_SHORT).show();
                    }
                });
                builder.setPositiveButton("确定",null);
                builder.create().show();
                break;

            case R.id.btn_RL:
                Calendar calendar = Calendar.getInstance();
                year = calendar.get(Calendar.YEAR);
                month = calendar.get(Calendar.MONTH);
                day = calendar.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog datePicker = new DatePickerDialog(DialogActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
                        mymonth = i1+1;
                        Toast.makeText(DialogActivity.this,"您选择了" +i+ "年" + mymonth + "月" + i2 + "日",Toast.LENGTH_SHORT).show();
                    }
                },year,month,day);
                datePicker.show();
                break;

            case R.id.btn_XZs:
                final String[] items1 = new String[]{"看电影","打游戏","听音乐","旅游","睡觉"};
                boolean[] ischecked = new boolean[]{false,false,false,false,false};
                AlertDialog.Builder builder1 = new AlertDialog.Builder(DialogActivity.this);
                builder1.setTitle("你的兴趣爱好是：");
                builder1.setMultiChoiceItems(items1,ischecked, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i, boolean ischecked) {
                        Toast.makeText(DialogActivity.this,items1[i]+":"+ischecked,Toast.LENGTH_SHORT).show();
                    }
                });
                break;

            case R.id.btn_ZDY:
                View view_pop = getLayoutInflater().inflate(R.layout.layout_pop,null);
                final PopupWindow popwin = new PopupWindow(view_pop, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
                popwin.setOutsideTouchable(true);
                popwin.showAsDropDown(view,200,50);
                break;

        }

    }
}